package com.superbytecode.stencil.entities.table;

import com.superbytecode.stencil.enums.MyBatisTypeEnum;
import com.superbytecode.stencil.utils.FieldUtil;
import com.superbytecode.stencil.utils.StringUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: code-stencil
 * @Package: com.superbytecode.stencil.structure.column
 * @ClassName: JavaColmun
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/14 18:01
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.cn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Data
@EqualsAndHashCode(callSuper = true)
public class JavaColumn extends SQLColumn {
    /**
     * java列名
     */
    final private String javaFieldName;
    /**
     * java列包装类型
     */
    final private String javaObjectType;
    /**
     * java列基本类型
     */
    final private String javaCommonType;
    /**
     * mybatis类型
     */
    final private String mybatisType;

    public static JavaColumn newInstance(final SQLColumn column) {
        JavaColumn javaColumn = new JavaColumn(column.getColumnName(), column.getColumnType());
        javaColumn.setComments(column.getComments());
        javaColumn.setIsIdentity(column.getIsIdentity());
        javaColumn.setIsPrimaryKey(column.getIsPrimaryKey());
        return javaColumn;
    }

    private JavaColumn(String columnName, String columnType) {
        this.setColumnName(columnName);
        this.setColumnType(columnType);
        this.javaFieldName = this.obtainJavaFieldName();
        this.javaCommonType = this.obtainJavaCommonType();
        this.javaObjectType = this.obtainJavaObjectType();
        this.mybatisType = this.obtainMybatisJdbcType();
    }

    // -----------------------------------------------------------------------------------------------------------------
    @Override
    public ColumnTypeConverter getColumnTypeConverter() {
        return new JavaColumnTypeConverter();
    }

    /*** 获得基本类型,int,float等 **/
    public String obtainJavaCommonType() {
        return this.getColumnTypeConverter().convertCommonType(this.getColumnType());
    }

    /*** 获得装箱类型,Integer,Float **/
    public String obtainJavaObjectType() {
        return this.getColumnTypeConverter().convertObjectType(this.getColumnType());
    }

    /*** 获取java字段，第一个字母大写 **/
    public String obtainJavaFieldNameUF() {
        return StringUtil.capitalizeFirstLetter(this.obtainJavaFieldName());
    }

    /**
     * 根据数据库字段映射java驼峰命名字段
     *
     * @return java字段
     */
    public String obtainJavaFieldName() {
        return FieldUtil.underscoreToCamelCase(this.getColumnName());
    }

    /*** 获取mybatis数据库字段 **/
    public String obtainMybatisJdbcType() {
        return MyBatisTypeEnum.ofMybatisType(this.getColumnType()).getFieldType();
    }

}